import { API_BASE_URL } from '@/config/apiConfig';
import { request } from '@/utils/request';

// 检查用户是否已登录
export const checkLogin = () => {
  const openid = uni.getStorageSync('token');
  return !!openid; // 返回布尔值，表示是否已登录
};

// 微信登录
export const wechatLogin = () => {
  return new Promise((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: (res) => {
        const code = res.code;
        // 发送code到后端
        request({
          url: `wechat/User/login`,
          method: 'POST',
          data: {
            platform: 'wechat',
            code: code
          }
        }).then(loginRes => {
       
          if (loginRes.code != 1) {
            
            uni.showToast({
              title: loginRes.msg,
              icon: 'none'
            });
          } else {
            // 存储token等信息
            uni.setStorageSync('openid', loginRes.data.openid);
          }

          resolve(loginRes);
        }).catch(err => {
       
          reject(err);
        });
      },
      fail: (err) => {
        uni.showToast({
          title: '获取code失败',
          icon: 'none'
        });
        reject(err);
      }
    });
  });
};
